﻿package com.hodgedev.net.drupal 
{
	import com.hodgedev.net.drupal.Service;
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	/**
	 * ...
	 * @author Brian Hodge
	 */
	 
	public class Comment extends Service
	{
		public static const SAVE				:String = "comment.save";
		public static const LOAD_NODE_COMMENTS	:String = "comment.loadNodeComments";
		
		private var _saveResponder:Responder;
		private var _loadNodeResponder:Responder;
		private var _comment:Object;
		private var _nid:String;
		private var _start:String;
		private var _end:String;
		private var _subject:String;
		private var _commentString:String;
		private var _data:Array;
		private var _mode:String;
		
		public function Comment(pServices:Services):void
		{
			_services = pServices;
			_sessid = _services.sessid;
			_nc = _services.netConnection;
			
			_saveResponder = new Responder(_onSaveCommentSuccess, _onSaveCommentError);
			_loadNodeResponder = new Responder(_onLoadNodeCommentSuccess, _onLoadNodeCommentError);
		}
		private function _onSaveCommentSuccess(pCID:int):void
		{
			closeConnection();
			dispatchEvent(new CommentEvent(CommentEvent.SAVE_COMPLETE, pCID));
		}
		private function _onSaveCommentError(obj:Object):void
		{
			closeConnection();
			trace("Comment Save Error");
		}
		private function _onLoadNodeCommentSuccess(obj:Array):void
		{
			_data = obj;
			closeConnection();
			dispatchEvent(new CommentEvent(CommentEvent.NODE_COMMENTS_READY, 0, obj));
		}
		private function _onLoadNodeCommentError(pObject:Object):void
		{
			for each (var item:* in pObject)
			{
				trace(item, pObject[item]);
			}
			closeConnection();
		}
		public function save(pNID:String, pSubject:String, pComment:String, pAuthor:String, pMail:String, pHomepage:String):void
		{
			_nid = pNID;
			_subject = pSubject;
			_commentString = pComment;
			_mode = Comment.SAVE;
			
			_comment = {};
			_comment.nid = _nid;
			_comment.subject = _subject;
			_comment.comment = _commentString;
			_comment.name = pAuthor;
			_comment.mail = pMail;
			_comment.homepage = pHomepage;
			
			if(_checkServiceConnected())
			{				
				_nc.call(Comment.SAVE, _saveResponder, _sessid, _comment);
			}
		}
		public function loadNodeComments(pNodeId:String, pStart:String = "0", pEnd:String = "0"):void
		{
			_nid = pNodeId;
			_mode = Comment.LOAD_NODE_COMMENTS;
			_start = pStart;
			_end = pEnd;
			
			if(_checkServiceConnected())
			{				
				_nc.call(Comment.LOAD_NODE_COMMENTS, _loadNodeResponder, _sessid, _nid, _start, _end);
			}
		}
		public override function onConnect(e:ServicesEvent):void
		{
			_services.removeEventListener(ServicesEvent.SYSTEM_CONNECTED, onConnect);
			_nc = _services.netConnection;
			_sessid = _services.sessid;
			
			switch(_mode)
			{
				case Comment.SAVE:
				_nc.call(Comment.SAVE, _saveResponder, _sessid, _comment);
				break;
				case Comment.LOAD_NODE_COMMENTS:
				_nc.call(Comment.LOAD_NODE_COMMENTS, _loadNodeResponder, _sessid, _nid, _start, _end);
				break;
			}
		}
		public function get data():Array
		{
			return _data;
		}
	}
}